package com.v9.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.dto.IdDto;
import com.v9.common.dto.PageInfoDto;
import com.v9.common.exception.BizException;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.Application;
import com.v9.system.domain.ApplicationRelease;
import com.v9.system.service.ApplicationReleaseService;
import com.v9.system.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Tag(name = "应用版本")
@Slf4j
@RequestMapping(value = "/sys/app/release", method = RequestMethod.POST)
public class ApplicationReleaseController extends AbstractController {

    @Resource
    private ApplicationReleaseService applicationReleaseService;
    @Resource
    private ApplicationService applicationService;

    @RequestMapping("/upload")
    @Operation(summary = "上传应用安装包")
    public void upload(@RequestBody ApplicationReleaseDto dto){
        applicationReleaseService.upload(dto.getUrl(),dto.getApplicationId(),dto.getVersionNumber(),dto.updateContent,dto.getForceUpdate());
    }


    @RequestMapping("/current")
    @Operation(summary = "获取当前最新版本信息")
    public ApplicationReleaseCheckDto current(@RequestBody CurrentQueryDto currentQueryDto){
        Application application = new Application();
        application.setCode(currentQueryDto.getCode());
        application = applicationService.findByOne(application);
        if (null == application){
            throw new BizException("获取更新信息失败,请检查应用配置");
        }
        QueryWrapper query=new QueryWrapper();
        query.eq("application_id",application.getId());
        query.eq("current_version",1);
        List<ApplicationRelease> releases =  applicationReleaseService.query(query);
        if (null == releases || releases.size() == 0){
            throw new BizException("获取更新信息失败,请检查应用配置");
        }
        ApplicationRelease release = releases.get(0);
        return clone(ApplicationReleaseCheckDto.class,release);
    }

    @RequestMapping("/set/current")
    @Operation(summary = "设置为当前版本")
    public void setCurrent(@RequestBody IdDto idDto){
        applicationReleaseService.setCurrent(idDto.getId());
    }

    @RequestMapping("/delete")
    @Operation(summary = "删除版本")
    public void delete(@RequestBody IdDto idDto){
        applicationReleaseService.remove(idDto.getId());
    }

    @RequestMapping("/list")
    @Operation(summary = "应用版本列表")
    public Page<ApplicationRelease> list (@RequestBody ApplicationReleaseQueryDto dto){
        ApplicationRelease applicationRelease = new ApplicationRelease();
        applicationRelease.setApplicationId(dto.getApplicationId());
        return applicationReleaseService.queryByPage(applicationRelease,dto.getPageable());
    }

    @RequestMapping("/force/update")
    @Operation(summary = "修改强制更新")
    public void forceUpdate(@Valid @RequestBody ApplicationReleaseForceUpdate dto){
        ApplicationRelease applicationRelease = new ApplicationRelease();
        applicationRelease.setId(dto.getId());
        applicationRelease.setForceUpdate(dto.getForceUpdate());
        applicationReleaseService.updateById(applicationRelease);
    }

    @Data
    public static class ApplicationReleaseDto{
        @NotNull(message = "文件上传地址不能为空")
        @Schema(description = "url")
        private String url;
        @NotNull(message = "应用ID不能为空")
        @Schema(description = "应用ID")
        private Long applicationId;

        @NotNull(message = "版本号不能为空")
        @Schema(description = "版本号")
        private String versionNumber;

        @Schema(description = "更新内容")
        private String updateContent;

        @NotNull
        @Schema(description = "是否强制更新")
        private Boolean forceUpdate;
    }

    @Data
    public static class CurrentQueryDto{
        @NotBlank
        @Schema(description = "应用编码")
        private String code;
    }

    @Data
    public static class ApplicationReleaseCheckDto{
        @Schema(description = "版本号")
        private String versionNumber;

        @Schema(description = "更新内容")
        private String updateContent;

        @Schema(description = "下载地址")
        private String downloadLink;

        @Schema(description = "是否强制更新 true 强制更新 false 不强制更新")
        private Boolean forceUpdate;

    }

    @Data
    public static class ApplicationReleaseQueryDto extends PageInfoDto {

        @NotNull(message = "应用ID不能为空")
        @Schema(description = "应用ID")
        private Long applicationId;
    }

    @Data
    public static class ApplicationReleaseForceUpdate{

        @NotNull(message = "版本ID不能为空")
        @Schema(description = "版本ID")
        private Long id;

        @NotNull(message = "强制更新不能为空")
        @Schema(description = "是否强制更新 true 强制更新 false 不强制更新")
        private Boolean forceUpdate;

    }

}
